﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Fly.Common.Linq;
using Telerik.Web.UI;
using RN.ERP.Web.ModelService;
using RN.ERP.Web.BasicService;

namespace RN.ERP.Web.PersonChange
{
    public partial class PersonChangeList : BasePage
    {
        #region  公共属性
        protected string CorpUserGuid
        {
            get
            {
                return UserInfo.CorpUserGuid;
            }
        }
        protected string AreaCode
        {
            get
            {
                return UserInfo.CurrentArea;
            }
        }
        protected string RoleType
        {
            get
            {
                return UserInfo.RoleType;
            }
        }
        /// <summary>
        /// 变更类型
        /// </summary>
        protected string ObjectType
        {
            get
            {
                return ViewState["objectType"].ToString();
            }
        }
        int classId = 0;
        /// 变更类型
        /// </summary>
        public int RoleID
        {
            get
            {
                int role = 0;
                if (Request["ClassID"] != null)
                {
                    int.TryParse(Request["RoleID"], out role);
                }
                return role;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initial();
                BindFlowCode();
            }
        }

        /// <summary>
        /// 参数及设置按钮显示
        /// </summary>
        private void Initial()
        {
            int objectType = 4;//变更类型
            ////人员类型
            if (Request.QueryString["classId"] != null)
            {
                int.TryParse(Request.QueryString["classId"], out classId);
            }
            if (Request.QueryString["objectType"] != null)
            {
                int.TryParse(Request.QueryString["objectType"].Trim(), out objectType);
            }
            ViewState["objectType"] = objectType;
            hdClassId.Value = classId.ToString();
            ////兼容企业端和主管端
            if (UserInfo.RoleType.ToLower() == "user" || UserInfo.RoleType.ToLower() == "expert")
            {

                UserManagerSelect.Visible = true;
                corpNameSearch.Visible = false;
            }
            else
            {
                ddlStatus.SelectedIndex = 1;
                UserManagerSelect.Visible = false;
                corpNameSearch.Visible = true;
            }
        }

        #region 人员列表
        /// <summary>
        /// 取得人员信息参数列表
        /// </summary>
        /// <returns></returns>
        private string GetQueryConditions()
        {

            List<ModelService.QueryPersonInfoCondition> query = new List<ModelService.QueryPersonInfoCondition>();
            ModelService.QueryPersonInfoCondition queryCondition = GetQueryCondition("1,");
            query.Add(queryCondition);
            return JsonConvert.SerializeObject(query);
        }
        /// <summary>
        /// 取得人员参数项
        /// </summary>
        /// <param name="flowCode"></param>
        /// <returns></returns>
        private ModelService.QueryPersonInfoCondition GetQueryCondition(string flowCode)
        {
            ///设置查询参数
            ModelService.QueryPersonInfoCondition queryCondition = new ModelService.QueryPersonInfoCondition();
            //queryCondition.TypeFlag = new ParameterValue<string> { V = "-1", O = OptEnum.Like };
            int.TryParse(hdClassId.Value, out classId);
            if (classId > 0)
            {
                queryCondition.ClassId = new ParameterValue<int?> { V = classId, O = OptEnum.Equal };
            }
            if (!string.IsNullOrEmpty(flowCode))
            {
                queryCondition.FlowCode = new ParameterValue<string> { V = flowCode, O = OptEnum.Like };
            }
            if (!string.IsNullOrEmpty(txtEmpName.Text.Trim()))
            {
                queryCondition.Name = new ParameterValue<string> { V = txtEmpName.Text.Trim(), O = OptEnum.Like };
            }
            if (!string.IsNullOrEmpty(txtEmpIdCard.Text.Trim()))
            {
                queryCondition.IDCard = new ParameterValue<string> { V = txtEmpIdCard.Text.Trim(), O = OptEnum.Equal };
            }
            //if (ObjectType != "4")
            //{
            //    queryCondition.StampCode = new ParameterValue<string> { V = "ZGZS", O = OptEnum.NotEqual };
            //}

            ///用户端登陆
            if (UserInfo.RoleType == "User" || UserInfo.RoleType == "Expert")
            {
                Guid userGuid = Guid.Empty;
                Guid.TryParse(UserInfo.CorpUserGuid, out userGuid);
                queryCondition.CorpGuid = new ParameterValue<Guid> { V = userGuid, O = OptEnum.Equal };
            }
            return queryCondition;
        }
        /// <summary>
        /// 取得人员基本信息
        /// </summary>
        /// <param name="empGuid"></param>
        /// <param name="areaCode"></param>
        private List<ModelService.tb_Emp_Manager> GetEmpManagersList(string areaCode, int currentPageIndex, int pageSize)
        {
            try
            {
                using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
                {
                    string queryStr = GetQueryConditions();

                    rgPersonList.VirtualItemCount = client.GetEmpManagerListCount(queryStr, areaCode);
                    string result = client.GetEmpManagersList(currentPageIndex + 1, pageSize, queryStr, areaCode);
                    return JsonConvert.DeserializeObject<List<ModelService.tb_Emp_Manager>>(result);
                }
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 绑定人员数据列表
        /// </summary>
        private void rgPersonDataBind()
        {
            int currentPageIndex = rgPersonList.CurrentPageIndex;
            int pageSize = rgPersonList.MasterTableView.PageSize;
            rgPersonList.DataSource = GetEmpManagersList(AreaCode, currentPageIndex, pageSize);

        }
        /// <summary>
        /// 绑定查询列表
        /// </summary>
        private void PersonDataBind()
        {
            rgPersonList.CurrentPageIndex = 0;
            rgPersonDataBind();
            rgPersonList.DataBind();
        }

        /// <summary>
        /// 绑定人员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void rgPersonList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            rgPersonDataBind();

        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnEmpSreach_Click(object sender, EventArgs e)
        {
            PersonDataBind();
        }

        #endregion

        #region 人员变更表

        /// <summary>
        /// 客户端grid刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "ChangeRebind")
            {
                this.rgChangeList.MasterTableView.SortExpressions.Clear();
                rgChangeList.MasterTableView.GroupByExpressions.Clear();
                rgChangeList.Rebind();
            }

        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnSearch_Click(object sender, EventArgs e)
        {
            GetPersonChangelist();
            rgChangeList.DataBind();
        }
        /// <summary>
        /// 审核状态查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlStatus_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            GetPersonChangelist();
            rgChangeList.DataBind();
        }

        /// <summary>
        /// 绑定人员基本信息变更表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void rgChangeList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            GetPersonChangelist();

        }
        /// <summary>
        /// 取得人员基本信息变更
        /// </summary>
        private void GetPersonChangelist()
        {
            using (BasicServiceClient client = new BasicServiceClient())
            {
                var count = client.GetChangeRecordsCount(GetChangeCondition(), UserInfo.CurrentArea);
                var result = client.GetChangeRecords(GetChangeCondition(), rgChangeList.CurrentPageIndex + 1, rgChangeList.PageSize, UserInfo.CurrentArea);
                var data = JsonConvert.DeserializeObject<List<V_ChangeInfo>>(result);
                rgChangeList.DataSource = data;
                rgChangeList.VirtualItemCount = count;
            }

        }

        #region 人员变更条件
        private QueryChangeInfoCondition GetCommonPersonChangeCondition()
        {
            QueryChangeInfoCondition con = new ModelService.QueryChangeInfoCondition();
            if (UserInfo.RoleType.ToLower() != "manager")
            {
                con.IsrtUserId = new ParameterValue<string>() { O = OptEnum.Equal, V = UserInfo.CorpUserGuid };
            }
            if (!string.IsNullOrEmpty(txtCorpName.Text.Trim()))
            {
                con.IsrtUserName = new ParameterValue<string>() { O = OptEnum.Like, V = txtCorpName.Text.Trim() };
            }
            if (!string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                con.ObjectName = new ParameterValue<string>() { O = OptEnum.Like, V = txtName.Text.Trim() };
            }
            string sysAreaCode = System.Configuration.ConfigurationManager.AppSettings["sysArea"];
            if (RoleID != 0)
                con.RoleID = new ParameterValue<int?>() { O = OptEnum.Equal, V = RoleID };
            con.AreaCode = new ParameterValue<string>() { O = OptEnum.Equal, V = sysAreaCode };
            con.SetAreaCode = new ParameterValue<string>() { O = OptEnum.Like, V = UserInfo.LocalArea };
            con.ObjectType = new ParameterValue<string>() { O = OptEnum.Equal, V = ObjectType };
            return con;
        }
        /// <summary>
        /// 取得查询条件
        /// </summary>
        /// <returns></returns>
        private string GetChangeCondition()
        {
            List<QueryChangeInfoCondition> list = new List<ModelService.QueryChangeInfoCondition>();
            string flowCodes = ddlStatus.SelectedValue;
            if (!string.IsNullOrEmpty(flowCodes))
            {
                foreach (var f in flowCodes.Split('|'))
                {
                    var con = GetCommonPersonChangeCondition();
                    con.FlowCode = new ParameterValue<string>() { O = OptEnum.Equal, V = f };
                    list.Add(con);
                }
            }
            else
            {
                var con = GetCommonPersonChangeCondition();
                if (UserInfo.RoleType.ToLower() == "manager")
                {
                    //管理端全部不包含未上报
                    con.FlowCode = new ParameterValue<string> { V = "019", O = OptEnum.Like };
                }
                list.Add(con);
            }

            return JsonConvert.SerializeObject(list);
        }
        #endregion
        #endregion

        private void BindFlowCode()
        {
            string classId = hdClassId.Value;
            if (string.IsNullOrEmpty(classId) || classId=="0")
            {
                classId = "8020";
            }
            string msg = string.Empty;
            var dic = UtilitiesTool.GetAllFlowStatus(classId, AreaCode, UserInfo.RoleId);
            ddlStatus.DataSource = dic;
            ddlStatus.DataTextField = "key";
            ddlStatus.DataValueField = "value";
            ddlStatus.DataBind();
        }

    }
}
